package com.hp.team.bookstore.model.profile;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

@Entity
@Table(name = "ROLE")
public class Role {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "ID")
	private long id;

	@Column(name = "NAME", nullable = false)
	private String name;

	@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	@JoinTable(name = "ROLE_PRIVILEGE", joinColumns = @JoinColumn(name = "ROLE_ID"), inverseJoinColumns = @JoinColumn(name = "PRIVILEGE_ID"))
	private Set<Privilege> privileges;

	public void setId(long id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setPrivileges(Set<Privilege> privileges) {
		this.privileges = privileges;
	}

	public long getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public Set<Privilege> getPrivileges() {
		return privileges;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Role) {
			Role otherRole = (Role) obj;
			if (id > 0) {
				return this.id == otherRole.id;
			} else {
				return this.name.equals(otherRole.name);
			}
		}
		return false;
	}
}
